<template>
    <div class="alarmConfiguration zy-content pd50">
        <back-level @click="close" />
        <!-- 模具与设备 -->
        <search :columns="searchData" @getSearch="getSearch(arguments)"></search>
        <div class="alarmConfiguration">
            <div class="list-layout mt16">
                <div class="btns clear">
                    <span class="title">{{ $t('common.listInformation') }}</span>
                    <Button @click="createfaultCodeShowH">{{ $t('common.New') }}</Button>
                    <!-- <Button @click="delFaultCodeShowClick">{{ $t('common.batchDelete') }}</Button> -->
                </div>
                <!-- 表格 -->
                <div class="tableContent">
                    <Table class="resizable-table" border @on-column-width-resize="onColumnWidthResize" :columns="columns"
                        ellipsis ref="selection" stripe :data="dataArr" @on-selection-change="onSelectChange">
                        <template slot-scope="{ row }" slot="action">
                            <span class="operationBtn" @click="delRow(row)"><i class="iconfont zy-common-shanchu"></i>{{
                                $t("common.Delete") }}</span>
                        </template>
                    </Table>
                </div>
                <!-- 分页 -->
                <div class="pageWrap mt16">
                    <pageLeftTitle :total="total" :selected="delIds.length" />
                    <Page :total="total" :page-size="pageSize" :page-size-opts="pageSizeOption" :current="pageNum"
                        @on-change="changePageNum" @on-page-size-change="changePageSize" show-sizer show-total />
                </div>
            </div>
        </div>
        <Modal :mask-closable="false" v-model="delFaultCodeShowF" :title="$t('common.Delete')" class="modal-title-layout">
            <div class="icon red">
                <i class="iconfont zy-common-shanchu"></i>
            </div>
            <p class="title">
                <span>{{ $t("common.confirmDelete") }}</span>
            </p>
            <div slot="footer">
                <Button type="default" @click="delFaultCodeShowF = false">{{
                    $t("common.Cancle")
                }}</Button>
                <Button type="primary" :loading="loading" @click="deleteOk">{{
                    $t("maintenance.Confirmed")
                }}</Button>
            </div>
        </Modal>
        <Modal :mask-closable="false" v-model="create" title="新增" class="modal-content-layout">
            <Form ref="form" class="formList" :model="formItem" label-position="top" :rules="ruleValidate">
                <FormItem label="检测项目" prop="checkProject">
                    <Input type="text" v-model="formItem.checkProject" :placeholder="`${$t('common.inputInfo')}`"
                        :maxlength="32"></Input>
                </FormItem>
                <FormItem label="检测内容" prop="checkContent">
                    <Input type="text" v-model="formItem.checkContent" :placeholder="`${$t('common.inputInfo')}`"
                        :maxlength="32"></Input>
                </FormItem>
                <FormItem :label="$t('common.remarks')" prop="remarks">
                    <Input type="textarea" v-model="formItem.remarks" :placeholder="`${$t('common.inputInfo')}`"
                        :autosize="{ minRows: 2, maxRows: 5 }"></Input>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button type="default" @click="create = false">{{
                    $t("common.Cancle")
                }}</Button>
                <Button type="primary" :loading="loading" @click="addOk('form')">{{
                    $t("maintenance.Confirmed")
                }}</Button>
            </div>
        </Modal>
    </div>
</template>
<script>
import search from "@/components/curd/searchList.vue";
import toggleTableOperation from "@/mixins/toggleTableOperation";
export default {
    name: "associationUser", //仓库配置
    components: { search },
    mixins: [toggleTableOperation],
    data() {
        return {
            addtitle: "",
            test: [],
            searchData: [
                {
                    title: '检测项目',
                    key: 'checkProject',
                    align: 'center'
                },
                {
                    title: '检测内容',
                    key: 'checkContent',
                    align: 'center'
                },
            ],
            formItem: {},
            //一级table配置
            columns: [
                {
                    key: "selection",
                    type: "selection",
                    width: 60,
                    align: "center",
                },
                {
                    title: '检测项目',
                    key: 'checkProject',
                    align: 'center'
                },
                {
                    title: '检测内容',
                    key: 'checkContent',
                    align: 'center'
                },
                {
                    title: "创建时间",
                    key: "createTime",
                    minWidth: 100,
                    align: "center",
                    resizable: true,
                    tooltip: true,
                },
                {
                    title: "创建人",
                    key: "createBy",
                    minWidth: 100,
                    align: "center",
                    resizable: true,
                    tooltip: true,
                },
                {
                    title: "操作",
                    slot: "action",
                    fixed: "right",
                    minWidth: 180,
                    align: "center",
                },
            ],
            ruleValidate: {
                checkContent: [
                    {
                        required: true,
                        message: "请输入检查内容",
                        trigger: "blur",
                        type: "string",
                    },
                ],
                checkProject: [
                    {
                        required: true,
                        message: "请输入检查项目",
                        trigger: "blur",
                        type: "string",
                    },
                ],
            },
            loading: false,
            //三级搜索参数
            searchParams: {},

            //一级页面分页参数
            pageNum: 1,
            pageSize: 10,
            total: 0,

            dataArr: [],
            userList: [],
            allCheck: false,
            create: false,
            delFaultCodeShowF: false, //是否显示删除弹窗
            pageSizeOption: [10, 20, 30, 40],
            delIds: [],
            selection: [],
            selectList: [],
        };
    },
    mounted() {
        this.getTableColumnsWidth(this.columns);
        this.searchList();
    },
    methods: {
        delFaultCodeShowClick() {//点击批量删除
            if (!this.delIds || this.delIds.length < 1) {
                this.$Message.warning(this.$t('common.selectInfo'));
                return;
            }
            this.delFaultCodeShowF = true;
        },
        addOk() {
            let params = {
                ...this.formItem,
                productionProcessesCode: this.$route.query.productionProcessesCode,
                createBy: localStorage.getItem('ziyunFactoryPortalName')
            };
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    this.axios({
                        method: 'post',
                        url: "/dms/clearLineConfig/save",
                        data: params,
                    }).then(res => {
                        this.loading = false;
                        if (res.code === 200) {
                            this.create = false;
                            this.searchList();
                        } else {
                            this.$Message.warning(({ content: res.msg, duration: 6, closable: true }));
                        }
                    });
                }
            })

        },
        getSearch(params) {
            this.searchParams = JSON.parse(JSON.stringify(params[0]));
            this.pageNum=1;
            this.searchList();
        },
        reset() {
            this.searchParams = {};
            this.changeNum();
        },
        //分页
        changeNum() {
            this.pageNum = 1;
            this.searchList();
        },
        changePageSize(pageSize) {
            this.pageSize = pageSize;
            this.searchList();
        },
        changePageNum(val) {
            this.pageNum = val;
            this.searchList();
        },
        createfaultCodeShowH(row) {//点击增加或编辑
            this.getUserList()
            // this.userKey = ''
            this.selectList = []
            this.create = true;
        },
        onSelectChange(selection) {
            let myArray = [];
            selection.map((item, index) => {
                myArray.push(item.id);
            });
            if (myArray.length > 0) {
                this.allCheck = true
            } else {
                this.allCheck = false
            }
            this.delIds = myArray;
        },
        //查询列表
        searchList() {
            let params = {
                pageNum: this.pageNum,
                pageSize: this.pageSize,
                productionProcessesCode: this.$route.query.productionProcessesCode,
            };
            for (let key in this.searchParams) {
                if (this.searchParams[key]) {
                    this.$set(params, key, this.searchParams[key]);
                }
            }
            this.axios({
                method: "post",
                url: '/dms/clearLineConfig/page',
                data: params,
            })
                .then((res) => {
                    if (res.code === 200) {
                        this.dataArr = res.data.records;
                        this.total = res.data.total;
                    } else {
                        this.$Message.warning({
                            content: res.msg,
                            duration: 6,
                            closable: true,
                        });
                    }
                });
        },
        getUserList(query) {
            this.axios({
                method: 'POST',
                url: '/dms/deviceUser/selectNotAssUserPage',
                data: {
                    pageNum: 1,
                    pageSize: 9999,
                    deviceCode: this.$route.query.deviceCode,
                    realname: query,
                }
            }).then(res => {
                if (res.code === 200) {
                    this.userList = res.data.records
                } else {
                    this.$Message.warning(({ content: res.msg, duration: 6, closable: true }));
                }
            })
        },
        delRow(row) {
            this.delFaultCodeShowF = true;
            this.delIds = [row.id];
            // this.targetRow = row;
        },
        deleteOk(ids) {
            this.loading = true;
            this.axios({
                method: 'DELETE',
                url: '/dms/clearLineConfig/delete',
                data: this.delIds,
            }).then(res => {
                this.loading = false;
                if (res.code === 200) {
                    this.searchList();
                    this.$Message.success("删除成功");
                    this.delFaultCodeShowF = false;
                    this.allCheck = false
                } else {
                    this.$Message.warning(({ content: res.msg, duration: 6, closable: true }));
                }
            })
        }
    },
};
</script>
      
<style scoped lang="scss">
.ivu-input-number {
    width: -webkit-fill-available;
}

.btn-box {
    padding: 8px 5px !important;
}
</style>